package com.example.innovate.controller;

/*
* 2021.09.28
* 毕业生就业信息表
* */

import com.example.innovate.bean.CounterPart;
import com.example.innovate.bean.Result;
import com.example.innovate.domain.AdminInfoDO;
import com.example.innovate.domain.EmploymentInfoDO;
import com.example.innovate.service.AdminService;
import com.example.innovate.service.EmploymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/api/employment")
@Api(tags = "毕业生就业接口")
public class EmploymentController {

    @Autowired
    EmploymentService employmentService;
    @Autowired
    AdminService adminService;

    @PostMapping("/addEmployment")
    @ApiOperation("添加毕业生就业信息")
    public Result addEmployment(@RequestBody EmploymentInfoDO employmentInfoDO,
                                 @RequestParam("userId") int userId) throws Exception{
        if(userId!=0){
            employmentInfoDO.setEmpUserId(userId);
        }
        employmentService.addInfo(employmentInfoDO);
        return new Result(200,"success","添加成功",employmentInfoDO);
    }

    @DeleteMapping("/deleteEmployment")
    @ApiOperation("删除毕业生就业信息")
    public Result deleteEmployment(@RequestParam("id") int id,
                                   String adminUserName) throws Exception{
        EmploymentInfoDO employmentInfoDO = employmentService.findById(id);
        if(employmentInfoDO!=null){
            if(adminUserName!=null){
                AdminInfoDO adminInfoDO = adminService.getAdminInfoByName(adminUserName);
                if(adminInfoDO!=null){
                    if(adminInfoDO.getAdminIsUpdate().equals(false)){
                        return new Result(400,"error","没有删除权限",employmentInfoDO);
                    }
                }
            }
            employmentService.deleteInfo(id);
            return new Result(200,"success","删除成功",employmentInfoDO);
        }
        else {
            return new Result(400,"error","信息不存在",employmentInfoDO);
        }

    }

    @PostMapping("/updateEmployment")
    @ApiOperation("修改毕业生就业信息")
    public Result updateEmployment(@RequestBody EmploymentInfoDO employmentInfoDO,
                                    String adminUserName) throws Exception{
        EmploymentInfoDO employmentInfoDO1 = employmentService.findById(employmentInfoDO.getEmpId());
        if(employmentInfoDO1!=null){
            if(adminUserName!=null){
                AdminInfoDO adminInfoDO = adminService.getAdminInfoByName(adminUserName);
                if (adminInfoDO!=null){
                    if(adminInfoDO.getAdminIsUpdate().equals(false)){
                        return new Result(400,"error","没有修改权限",employmentInfoDO);
                    }
                }
            }
            employmentService.updateInfo(employmentInfoDO);
            return new Result(200,"success","修改成功",employmentInfoDO);
        }
        else {
            return new Result(400,"error","信息不存在",employmentInfoDO);
        }
    }

    @GetMapping("/findEmployment")
    @ApiOperation("查找毕业生就业信息")
    public Result findAllEmployment(@RequestParam  Integer pageNumber,
                                    @RequestParam  Integer userId,
                                    @RequestParam Integer pageSize,
                                    String className,
                                    String name) throws Exception{
        if(className!=null){
            if (className.equals("superAdmin")){
                className=null;
                List<EmploymentInfoDO> list = employmentService.findAll(className,name,userId,pageNumber,pageSize);
                return new Result(200,"success","查找成功",list);
            }
            else {
                List<EmploymentInfoDO> list = employmentService.findAll(className,name,userId,pageNumber,pageSize);
                return new Result(200,"success","查找成功",list);
            }
        }
        else{
            List<EmploymentInfoDO> list = employmentService.findAll(className,name,userId,pageNumber,pageSize);
            return new Result(200,"success","查找成功",list);
        }

    }

    @GetMapping("/getMajor")
    @ApiOperation("获取库中现存所有专业")
    public Result getMajor() throws Exception{
        Set<String> set = employmentService.findAllMajor();
        return new Result(200,"success","查找成功",set);
    }

    @GetMapping("/chartMajor")
    @ApiOperation("图表展示-根据所属专业展示就业占比率")
    public Result chartMajor(@RequestParam String major) throws Exception{
        double count = employmentService.proportionByMajor(major);
        return new Result(200,"success","查找成功",count);
    }

    @GetMapping("/chartAll")
    @ApiOperation("图表展示-所有专业就业占比率")
    public Result chartMajor() throws Exception{
        Map<String,Double> map = new HashMap<>();
        Set<String> set = employmentService.findAllMajor();
        for(String major:set){
            double count = employmentService.proportionByMajor(major);
            map.put(major,count);
        }
                return new Result(200,"success","查找成功",map);
    }

    @GetMapping("/chartCounterPart")
    @ApiOperation("图表展示-专业对口情况")
    public Result chart(String major) throws Exception{
        CounterPart counterPart = employmentService.findInfo(major);
        return new Result(200,"success","查找成功",counterPart);
    }

    @GetMapping("/chartCity")
    @ApiOperation("图表展示-就业城市")
    public Result city() throws Exception{
        Map<String, Integer> map = employmentService.findCity();
        return new Result(200,"success","查找成功",map);
    }

    @GetMapping("/chartPay")
    @ApiOperation("图表展示-薪酬占比")
    public Result pay() throws Exception{
        Map<String, Double> map = employmentService.findPay();
        return new Result(200,"success","查找成功",map);
    }



}
